﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DALFactory;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class DriverBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IDriverDA DriverDA = DataAccess.CreateDriverDA();
        private PubParasInfo pubParasInfo;

        public DriverBLL()
        {
            this.pubParasInfo = null;
        }

        public DriverBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public DriverBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<DriverInfo> driverInfos = new List<DriverInfo>();
                DriverInfo driverInfo = null;
                foreach (string id in IDS)
                {
                    driverInfo = DriverDA.GetDriverByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    driverInfos.Add(driverInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (DriverDA.DeleteDriver(dataBase, tran, IDS, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<DriverInfo>(dataBase, tran, driverInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "DRIVER", this.dataBaseID);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return DriverDA.GetAllDriver(this.dataBaseID[0], this.pubParasInfo);
        }

        public object GetInfoByID(string id)
        {
            return DriverDA.GetDriverByID(id, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            DriverQueryEntity driverQuery = obj as DriverQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(driverQuery.DriverID))
            {
                sqlWhere.Add(" DRIVER_ID " + driverQuery.DriverID);
            }
            if (!string.IsNullOrEmpty(driverQuery.DriverName))
            {
                sqlWhere.Add(" DRIVER_NAME " + driverQuery.DriverName);
            }
            if (!string.IsNullOrEmpty(driverQuery.Sex))
            {
                sqlWhere.Add(" SEX " + driverQuery.Sex);
            }
            if (!string.IsNullOrEmpty(driverQuery.Age))
            {
                sqlWhere.Add(" AGE " + driverQuery.Age);
            }
            if (!string.IsNullOrEmpty(driverQuery.IdCard))
            {
                sqlWhere.Add(" ID_CARD " + driverQuery.IdCard);
            }
            if (!string.IsNullOrEmpty(driverQuery.DriverCardNo))
            {
                sqlWhere.Add(" DRIVER_CARD_NO " + driverQuery.DriverCardNo);
            }
            if (!string.IsNullOrEmpty(driverQuery.DriverCardType))
            {
                sqlWhere.Add(" DRIVER_CARD_TYPE " + driverQuery.DriverCardType);
            }
            if (!string.IsNullOrEmpty(driverQuery.Telephone))
            {
                sqlWhere.Add(" TELEPHONE " + driverQuery.Telephone);
            }
            if (!string.IsNullOrEmpty(driverQuery.Address))
            {
                sqlWhere.Add(" ADDRESS " + driverQuery.Address);
            }
            if (!string.IsNullOrEmpty(driverQuery.Zip))
            {
                sqlWhere.Add(" ZIP " + driverQuery.Zip);
            }
            if (!string.IsNullOrEmpty(driverQuery.Province))
            {
                sqlWhere.Add(" PROVINCE " + driverQuery.Province);
            }
            if (!string.IsNullOrEmpty(driverQuery.City))
            {
                sqlWhere.Add(" CITY " + driverQuery.City);
            }
            if (!string.IsNullOrEmpty(driverQuery.Status))
            {
                sqlWhere.Add(" DR.STATUS " + driverQuery.Status);
            }
            if (!string.IsNullOrEmpty(driverQuery.IsActive))
            {
                sqlWhere.Add(" DR.IS_ACTIVE " + driverQuery.IsActive);
            }
            return DriverDA.GetDriverByQueryList(sqlWhere, driverQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public string Insert(object obj)
        {
            string driverID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                DriverInfo driverInfo = obj as DriverInfo;
                if (DriverDA.CheckDriverIDUnique(dataBase, tran, driverInfo.DriverID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2510_001", this.pubParasInfo.UserLanguage, new List<string> { driverInfo.DriverID });
                }
                ICarrierDA carrierDA = DataAccess.CreateCarrierDA();
                if (!carrierDA.CheckCarrierIDUnique(dataBase, tran, driverInfo.CarrierID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2520_002", this.pubParasInfo.UserLanguage);
                }
                else if (carrierDA.CheckAuthorizationCarrierUserID(dataBase, tran, driverInfo.CarrierID, this.dataBaseID[0], this.pubParasInfo))
                {
                    PublicMethod.GetInstance().ShowAlertMessage(" F2140_002", this.pubParasInfo.UserLanguage, new List<string> { driverInfo.CarrierID });
                }
                driverInfo.Status = DriverStatus.FREE;
                driverInfo.UpdatedBy = driverInfo.CreatedBy;
                driverInfo.CreatedDate = DateTime.Now;
                driverInfo.UpdatedDate = DateTime.Now;
                if (DriverDA.InsertDriver(dataBase, tran, driverInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<DriverInfo>(dataBase, tran, null, driverInfo, AuditAction.Insert, this.pubParasInfo.UserID, "DRIVER", this.dataBaseID[0]);
                    driverID = driverInfo.DriverID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return driverID;
        }

        public string Update(object obj)
        {
            string driverID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                DriverInfo driverInfo = obj as DriverInfo;
                ICarrierDA carrierDA = DataAccess.CreateCarrierDA();
                if (!carrierDA.CheckCarrierIDUnique(dataBase, tran, driverInfo.CarrierID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2520_002", this.pubParasInfo.UserLanguage);
                }
                else if (carrierDA.CheckAuthorizationCarrierUserID(dataBase, tran, driverInfo.CarrierID, this.dataBaseID[0], this.pubParasInfo))
                {
                    PublicMethod.GetInstance().ShowAlertMessage(" F2140_002", this.pubParasInfo.UserLanguage, new List<string> { driverInfo.CarrierID });
                }
                driverInfo.UpdatedDate = DateTime.Now;
                DriverInfo oldDriverInfo = DriverDA.GetDriverByID(dataBase, tran, driverInfo.DriverID, this.dataBaseID[0], this.pubParasInfo);
                if (DriverDA.UpdateDriver(dataBase, tran, driverInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<DriverInfo>(dataBase, tran, oldDriverInfo, driverInfo, AuditAction.Update, this.pubParasInfo.UserID, "DRIVER", this.dataBaseID);
                    driverID = driverInfo.DriverID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return driverID;
        }
    }
}

